﻿using Blog.Core.Model.Model;
using Blog.Core.Model.ViewModel;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Blog.Core.AuthHelper
{
    public class JwtHelperServices : IJwtHelperServices
    {
        private readonly IConfiguration _configuration;
        public JwtHelperServices(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public string IssuerToken(UserRoleDTO userRoleDTO)
        {
            var claims = new Claim[] {
                            new Claim(ClaimTypes.Name, userRoleDTO.UserName),
                            new Claim(ClaimTypes.NameIdentifier, userRoleDTO.Password),
                            new Claim(ClaimTypes.Role,userRoleDTO.RoleName ?? "default")
                        };
            var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(_configuration["SecurityKey"]));
            var expires = DateTime.Now.AddDays(7);
            var token = new JwtSecurityToken(
                issuer: _configuration["issuer"],
                audience: _configuration["audience"],
                claims: claims,
                notBefore: DateTime.Now,
                expires: expires,
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256));
            return new JwtSecurityTokenHandler().WriteToken(token);
        }
    }
}
